#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022, Andrey Kunitsyn
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

def init(module):
    module.name = ":board:rp-pico"
    module.description = """\
# Raspberry Pi Pico

RP2040 Official System Development Board
See https://www.raspberrypi.com/products/raspberry-pi-pico


## Programming

### UF2

The RP2040 ships with a [UF2 bootloader in ROM](https://github.com/microsoft/uf2).

To upload your application, connect the RP2040 via USB, convert the ELF to UF2
format by calling `scons uf2` or `make uf2` and copy the generated `.uf2` file
to the mounted virtual disk.


### OpenOCD

Another option is to use Picoprobe and custom version of OpenOCD provided by RPi.

You would need to build and program one of the boards with the Picoprobe firmware,
configure and build RPi OpenOCD, and connect the boards via SWD.
The wiring diagram and detailed description is available in the
[Pico Documentation](https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf#Appendix%20A:%20Using%20Picoprobe).

The how-to shows steps for building and installing the RPi OpenOCD into your
system globally, however, if you still want to be able to work with other OpenOCD
binaries, you should only install it locally:

```sh
sudo apt install automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0-dev

cd ~/pico
git clone https://github.com/raspberrypi/openocd.git --branch rp2040 --depth=1 --no-single-branch

cd openocd
./bootstrap

mkdir build
cd build
../configure --enable-picoprobe --prefix=$(realpath ../install)

make -j4
make install
```

[Additional steps are needed](https://forums.raspberrypi.com/viewtopic.php?t=312867)
to get access to the device from a non-privileged user on Linux.
Edit or create the `/etc/udev/rules.d/60-openocd.rules` file and append these
lines to it:

```
# Raspberry Pi Picoprobe
ATTRS{idVendor}=="2e8a", ATTRS{idProduct}=="0004", MODE="660", GROUP="plugdev", TAG+="uaccess"
```

Once you have both of your Pico boards connected according to the wiring diagram,
you can run `scons program` to program the dev board:

```sh
export MODM_OPENOCD_BINARY=$HOME/pico/openocd/install/bin/openocd
scons program
```
"""

def prepare(module, options):
    if not options[":target"].partname.startswith("rp2040"):
        return False

    module.depends(
        ":platform:clock",
        ":platform:core",
        ":platform:gpio",
        ":platform:usb",
        ":platform:clockgen")
    return True

def build(env):
    env.outbasepath = "modm/src/modm/board"
    env.substitutions = {
        "with_logger": False,
        "with_assert": env.has_module(":architecture:assert")
    }
    env.template("../board.cpp.in", "board.cpp")
    env.copy('.')
    env.collect(":build:openocd.source", "interface/picoprobe.cfg", "target/rp2040.cfg")
